package cn.valorin.dueltime.ranking.hologram;

import cn.valorin.dueltime.DuelTimePlugin;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;

import java.util.ArrayList;
import java.util.List;

public class HologramInstance {
    private Object hologram;
    private final HologramPluginType hologramPluginType;

    public HologramInstance(HologramPluginType hologramPluginType, Location location, String rankingId, List<String> content, Material material) {
        this.hologramPluginType = hologramPluginType;
        if (hologramPluginType == HologramPluginType.HOLOGRAPHIC_DISPLAY) {
            this.hologram = com.gmail.filoghost.holographicdisplays.api.HologramsAPI.createHologram(DuelTimePlugin.getInstance(), location);
            ((com.gmail.filoghost.holographicdisplays.api.Hologram) this.hologram).appendItemLine(new ItemStack(material));
            content.forEach(((com.gmail.filoghost.holographicdisplays.api.Hologram) this.hologram)::appendTextLine);
        } else if (hologramPluginType == HologramPluginType.CMI) {
            com.Zrips.CMI.Modules.Holograms.CMIHologram hologram = new com.Zrips.CMI.Modules.Holograms.CMIHologram(rankingId, location);
            hologram.setLoc(location);
            hologram.setLines(content);
            hologram.refresh();
            this.hologram = hologram;
        } else if (hologramPluginType == HologramPluginType.DECENT_HOLOGRAM) {
            //这个插件的全息图id不允许包括冒号
            eu.decentsoftware.holograms.api.holograms.Hologram hologram = eu.decentsoftware.holograms.api.DHAPI.createHologram(rankingId.replace("dueltime:", ""), location, null);
            if (material != null) {
                eu.decentsoftware.holograms.api.DHAPI.addHologramLine(hologram, material);
            }
            content.forEach(line -> eu.decentsoftware.holograms.api.DHAPI.addHologramLine(hologram, line));
            this.hologram = hologram;
        }
    }

    public void destroy() {
        switch (hologramPluginType) {
            case HOLOGRAPHIC_DISPLAY:
                ((com.gmail.filoghost.holographicdisplays.api.Hologram) hologram).delete();
                break;
            case CMI:
                ((com.Zrips.CMI.Modules.Holograms.CMIHologram) hologram).remove();
                break;
            case DECENT_HOLOGRAM:
                ((eu.decentsoftware.holograms.api.holograms.Hologram) hologram).delete();
        }
    }

    public void refresh(List<String> content, Material material) {
        switch (hologramPluginType) {
            case HOLOGRAPHIC_DISPLAY:
                Bukkit.getScheduler().runTask(DuelTimePlugin.getInstance(), () -> {
                    ((com.gmail.filoghost.holographicdisplays.api.Hologram) hologram).clearLines();
                    ((com.gmail.filoghost.holographicdisplays.api.Hologram) hologram).appendItemLine(new ItemStack(material));
                    content.forEach((((com.gmail.filoghost.holographicdisplays.api.Hologram) hologram))::appendTextLine);
                });
                break;
            case CMI:
                ((com.Zrips.CMI.Modules.Holograms.CMIHologram) hologram).setLines(content);
                ((com.Zrips.CMI.Modules.Holograms.CMIHologram) hologram).refresh();
                break;
            case DECENT_HOLOGRAM:
                Bukkit.getScheduler().runTask(DuelTimePlugin.getInstance(), () -> {
                    eu.decentsoftware.holograms.api.holograms.Hologram hologram = (eu.decentsoftware.holograms.api.holograms.Hologram) this.hologram;
                    eu.decentsoftware.holograms.api.DHAPI.setHologramLines(hologram, new ArrayList<>());
                    if (material != null) {
                        eu.decentsoftware.holograms.api.DHAPI.addHologramLine(hologram, material);
                    }
                    content.forEach(line -> eu.decentsoftware.holograms.api.DHAPI.addHologramLine(hologram, line));
                });
        }
    }


    public void move(Location location) {
        switch (hologramPluginType) {
            case HOLOGRAPHIC_DISPLAY:
                ((com.gmail.filoghost.holographicdisplays.api.Hologram) hologram).teleport(location);
                break;
            case CMI:
                ((com.Zrips.CMI.Modules.Holograms.CMIHologram) hologram).setLoc(location);
                ((com.Zrips.CMI.Modules.Holograms.CMIHologram) hologram).refresh();
                break;
            case DECENT_HOLOGRAM:
                eu.decentsoftware.holograms.api.DHAPI.moveHologram((eu.decentsoftware.holograms.api.holograms.Hologram) hologram, location);
        }
    }
}
